import Footer from './Footer'
import { applyApi } from '../../../../common/api/applyManage'
import Bus from '@/common/bus'

export default {
    components: {
        Footer,
    },
    data() {
        return {
            applyApi,
            Bus,
            isShowSave: this.record?.cjzt ? [1].includes(this.record.cjzt) : true,
        }
    },
    mounted() {},
    props: {
        record: {
            type: Object,
            required: false,
        },
        isEdit: {
            type: Boolean,
            required: false,
        },
    },
    methods: {
        back() {
            this.$emit('updateState', 'back')
        },
        previousStep() {
            if (this.isShowSave) {
                this.save('previousStep')
            } else {
                this.$emit('updateState', 'previousStep')
            }
        },

        nextStep() {
            if (this.isShowSave || this.isShowSubmit) {
                this.save('nextStep')
            } else {
                this.$emit('updateState', 'nextStep')
            }
        },
        async save(direction) {
            let id = null
            if (this.record?.id) {
                id = this.record.id
            } else {
                id = sessionStorage.getItem('applyRecordId')
            }
            this.$refs.form.validate(async valid => {
                if (valid) {
                    if (this.saveApi instanceof Function) {
                        let res = await this.saveApi(id)
                        if (res.code == 200) {
                            this.$emit('updateState', direction)
                        }
                    }
                } else {
                    return false
                }
            })
        },
    },
}
